#include "linkageConditionTable.h"
#include "version.h"
#include "logger.h"
#include "db.h"
#include <string.h>
#include <stdlib.h>

/**
 *  
 */
#define LINKAGE_CONDITION_TABLE_NAME "t_linkage_condition"
#define SQL_LINKAGE_CONDITION_TABLE_CREATE "CREATE TABLE IF NOT EXISTS " LINKAGE_CONDITION_TABLE_NAME " (id INT PRIMARY KEY NOT NULL, linkageID INT NOT NULL, deviceID INT NOT NULL, pointID TEXT NOT NULL, op TEXT NOT NULL, value TEXT)"
#define SQL_LINKAGE_CONDITION_TABLE_INSERT(...) "INSERT INTO " LINKAGE_CONDITION_TABLE_NAME "(id, linkageID, deviceID, pointID, op, value) VALUES (" #__VA_ARGS__ ")"
#define SQL_LINKAGE_CONDITION_TABLE_REPLACE(...) "REPLACE INTO " LINKAGE_CONDITION_TABLE_NAME "(id, linkageID, deviceID, pointID, op, value) VALUES (" #__VA_ARGS__ ")"
#define SQL_LINKAGE_CONDITION_TABLE_DELETE(id) "DELETE FROM " LINKAGE_CONDITION_TABLE_NAME " WHERE id = " #id
#define SQL_LINKAGE_CONDITION_TABLE_SELECT(id) "SELECT * FROM " LINKAGE_CONDITION_TABLE_NAME " WHERE id = " #id



/*===============================================================*/
/* 联动 */
int createLinkageConditionTable(){
    return db_create_table(SQL_LINKAGE_CONDITION_TABLE_CREATE);
}

int insertLinkageCondition(T_LINKAGE_CONDITION* condition){
    if(NULL == condition){
        log_file_e(APPNAME, "insert linkage condition is NULL");
        return -1;
    }
    int ret = db_insert(SQL_LINKAGE_CONDITION_TABLE_INSERT(condition->id, condition->linkageID, condition->deviceID, condition->pointID, condition->op, condition->value));
    return ret;
}

int replaceLinkageCondition(T_LINKAGE_CONDITION* condition){
    if(NULL == condition){
        log_file_e(APPNAME, "replace linkage condition is NULL");
        return -1;
    }
    int ret = db_insert(SQL_LINKAGE_CONDITION_TABLE_REPLACE(condition->id, condition->linkageID, condition->deviceID, condition->pointID, condition->op, condition->value));
    return ret;
}

int deleteLinkageCondition(T_LINKAGE_CONDITION* condition){
    if(NULL == condition){
        log_file_e(APPNAME, "delete linkage condition is NULL");
        return -1;
    }
    return db_delete(SQL_LINKAGE_CONDITION_TABLE_DELETE(condition->id));
}
/**
 *  
 */
static int linkageConditionCallback(void *data, int argc, char **argv, char **azColName){
    int i = 0;
    if(NULL == data){
        log_file_e(APPNAME, "linkageConditionCallback data is NULL");
        return -1;
    }
    T_LINKAGE_CONDITION_LIST* linkageConditionList = (T_LINKAGE_CONDITION_LIST*)data;
    if((0 == linkageConditionList->count) || (NULL == linkageConditionList->head)){
        linkageConditionList->head = linkageConditionList->tail = (T_LINKAGE_CONDITION*)malloc(sizeof(T_LINKAGE_CONDITION));
        linkageConditionList->head->next = NULL;
        linkageConditionList->head->prev = NULL;
    } else {
        linkageConditionList->tail->next = (T_LINKAGE_CONDITION*)malloc(sizeof(T_LINKAGE_CONDITION));
        linkageConditionList->tail->next->prev = linkageConditionList->tail;
        linkageConditionList->tail = linkageConditionList->tail->next;
    }
    linkageConditionList->count++;
    for(i = 0; i < argc; i++){
        if(0 == strcmp(azColName[i], "id")){
            linkageConditionList->tail->id = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "linkageID")){
            linkageConditionList->tail->linkageID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "deviceID")){
            linkageConditionList->tail->deviceID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "pointID")){
            linkageConditionList->tail->pointID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "op")){
            if(NULL == argv[i]){
                linkageConditionList->tail->op[0] = '\0';
            } else {
                strcpy(linkageConditionList->tail->op, argv[i]);
            }
        } else if(0 == strcmp(azColName[i], "value")){
            if(NULL == argv[i]){
                linkageConditionList->tail->value[0] = '\0';
            } else {
                strcpy(linkageConditionList->tail->value, argv[i]);
            }
        }
        log_i("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return linkageConditionList->count;
}

int selectLinkageCondition(const char* sql, T_LINKAGE_CONDITION_LIST* linkageConditionList){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == linkageConditionList)){
        log_file_e(APPNAME, "select linkage condition sql is NULL");
        return -1;
    }
    return db_select(sql, linkageConditionCallback, linkageConditionList);

}
//
void dbLinkageConditionInit(){
    createLinkageConditionTable();
}
